package tech.lq0.dreamaticvoyage.gui.menu;

import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.capabilities.ForgeCapabilities;
import tech.lq0.dreamaticvoyage.block.entity.FukamizuFermenterBlockEntity;
import tech.lq0.dreamaticvoyage.gui.slot.ContainerEnergyData;
import tech.lq0.dreamaticvoyage.init.MenuTypeRegistry;

public class FukamizuFermenterMenu extends BaseEnergyMenu {

    public final FukamizuFermenterBlockEntity blockEntity;
    private final ContainerEnergyData containerData;

    public static final int X_OFFSET = 0;
    public static final int Y_OFFSET = 11;

    public FukamizuFermenterMenu(int id, Inventory inventory, FukamizuFermenterBlockEntity blockEntity, ContainerEnergyData containerData) {
        super(MenuTypeRegistry.FUKAMIZU_FERMENTER_MENU.get(), id, containerData);

        checkContainerSize(blockEntity, 4);

        this.blockEntity = blockEntity;
        this.containerData = containerData;

        this.addSlot(new InputSlot(blockEntity, 0, 39, 32));
        this.addSlot(new InputSlot(blockEntity, 1, 39, 53));
        this.addSlot(new BucketSlot(blockEntity, 2, 128, 30));
        this.addSlot(new ResultSlot(blockEntity, 3, 128, 56));

        for (int i = 0; i < 3; ++i) {
            for (int j = 0; j < 9; ++j) {
                this.addSlot(new Slot(inventory, j + i * 9 + 9, 8 + j * 18 + X_OFFSET, 84 + i * 18 + Y_OFFSET));
            }
        }

        for (int k = 0; k < 9; ++k) {
            this.addSlot(new Slot(inventory, k, 8 + k * 18 + X_OFFSET, 142 + Y_OFFSET));
        }
    }

    @Override
    public ItemStack quickMoveStack(Player pPlayer, int pIndex) {
        ItemStack itemstack = ItemStack.EMPTY;
        Slot slot = this.slots.get(pIndex);
        if (slot.hasItem()) {
            ItemStack itemstack1 = slot.getItem();
            itemstack = itemstack1.copy();
            if (pIndex <= 3) {
                if (!this.moveItemStackTo(itemstack1, 4, 40, true)) {
                    return ItemStack.EMPTY;
                }

                slot.onQuickCraft(itemstack1, itemstack);
            } else {
                if (itemstack1.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).isPresent()) {
                    if (!this.moveItemStackTo(itemstack1, 2, 3, false)) {
                        return ItemStack.EMPTY;
                    }
                }
                if (!this.moveItemStackTo(itemstack1, 0, 1, false)) {
                    if (!this.moveItemStackTo(itemstack1, 1, 2, false)) {
                        return ItemStack.EMPTY;
                    }
                    return ItemStack.EMPTY;
                } else if (pIndex < 31) {
                    if (!this.moveItemStackTo(itemstack1, 31, 40, false)) {
                        return ItemStack.EMPTY;
                    }
                } else if (pIndex < 40 && !this.moveItemStackTo(itemstack1, 4, 31, false)) {
                    return ItemStack.EMPTY;
                }
            }

            if (itemstack1.isEmpty()) {
                slot.setByPlayer(ItemStack.EMPTY);
            } else {
                slot.setChanged();
            }

            if (itemstack1.getCount() == itemstack.getCount()) {
                return ItemStack.EMPTY;
            }

            slot.onTake(pPlayer, itemstack1);
        }

        return itemstack;
    }

    @Override
    public boolean stillValid(Player pPlayer) {
        return this.blockEntity.stillValid(pPlayer);
    }

    public long getEnergy() {
        return this.containerData.get(0);
    }

    public long getTempAmount() {
        return this.containerData.get(1);
    }

    public long getTempCapacity() {
        return this.containerData.get(2);
    }

    static class InputSlot extends Slot {

        public InputSlot(Container pContainer, int pSlot, int pX, int pY) {
            super(pContainer, pSlot, pX, pY);
        }

        public boolean mayPlace(ItemStack pStack) {
            return super.mayPlace(pStack);
        }

        public int getMaxStackSize() {
            return 64;
        }
    }

    static class BucketSlot extends Slot {

        public BucketSlot(Container pContainer, int pSlot, int pX, int pY) {
            super(pContainer, pSlot, pX, pY);
        }

        public boolean mayPlace(ItemStack pStack) {
            return pStack.getCapability(ForgeCapabilities.FLUID_HANDLER_ITEM).isPresent();
        }

        public int getMaxStackSize() {
            return 1;
        }
    }

    static class ResultSlot extends Slot {

        public ResultSlot(Container pContainer, int pSlot, int pX, int pY) {
            super(pContainer, pSlot, pX, pY);
        }

        public boolean mayPlace(ItemStack pStack) {
            return false;
        }

        public int getMaxStackSize() {
            return 1;
        }
    }
}
